﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;

namespace Gmantis.Controls
{
    internal class MenuItemFirstLevel : MenuItemBehavior
    {
        // Methods
        public MenuItemFirstLevel(MenuItem item)
            : base(item)
        {
        }

        public override MenuItem GetFirstItem()
        {
            return base.GetNextEnabledItem(base.MenuItem.MenuItems, 0);
        }

        public override MenuItem GetLastItem()
        {
            return base.GetPrevEnabledItem(base.MenuItem.MenuItems, base.MenuItem.MenuItems.Count - 1);
        }

        public override MenuItem GetNextItem(Orientation orientation)
        {
            switch (orientation)
            {
                case Orientation.Horizontal:
                    {
                        IList<MenuItem> parentContainers = base.MenuItem.GetParentContainers<MenuItem>();
                        MenuItem nextEnabledItem = base.GetNextEnabledItem(parentContainers, (base.MenuItem.Index + 1) % parentContainers.Count);
                        if (nextEnabledItem == null)
                        {
                            return base.MenuItem;
                        }
                        MenuItem item2 = base.GetNextEnabledItem(nextEnabledItem.MenuItems, 0);
                        if (item2 != null)
                        {
                            return item2;
                        }
                        CancelSourceEventArgs e = new CancelSourceEventArgs
                        {
                            Source = nextEnabledItem,
                            Cancel = false
                        };
                        nextEnabledItem.OnSubmenuOpening(e);
                        return nextEnabledItem;
                    }
            }
            return (base.GetNextEnabledItem(base.MenuItem.MenuItems, 0) ?? base.MenuItem);
        }

        public override List<AnchorMatching> GetPreferedLocations()
        {
            if (base.MenuItem.Menu.Orientation == Orientation.Horizontal)
            {
                List<AnchorMatching> list = new List<AnchorMatching>();
                AnchorMatching item = new AnchorMatching
                {
                    FixedElementAnchor = Anchor.BottomLeft,
                    MoveableElementAnchor = Anchor.TopLeft
                };
                list.Add(item);
                AnchorMatching matching2 = new AnchorMatching
                {
                    FixedElementAnchor = Anchor.BottomRight,
                    MoveableElementAnchor = Anchor.TopRight
                };
                list.Add(matching2);
                AnchorMatching matching3 = new AnchorMatching
                {
                    FixedElementAnchor = Anchor.TopLeft,
                    MoveableElementAnchor = Anchor.BottomLeft
                };
                list.Add(matching3);
                AnchorMatching matching4 = new AnchorMatching
                {
                    FixedElementAnchor = Anchor.TopRight,
                    MoveableElementAnchor = Anchor.BottomRight
                };
                list.Add(matching4);
                AnchorMatching matching5 = new AnchorMatching
                {
                    FixedElementAnchor = Anchor.BottomLeft,
                    MoveableElementAnchor = Anchor.TopLeft
                };
                list.Add(matching5);
                return list;
            }
            return base.GetPreferedLocations();
        }

        public override MenuItem GetPrevItem(Orientation orientation)
        {
            switch (orientation)
            {
                case Orientation.Horizontal:
                    {
                        IList<MenuItem> parentContainers = base.MenuItem.GetParentContainers<MenuItem>();
                        MenuItem prevEnabledItem = base.GetPrevEnabledItem(parentContainers, (base.MenuItem.Index == 0) ? (parentContainers.Count - 1) : (base.MenuItem.Index - 1));
                        if (prevEnabledItem == null)
                        {
                            return base.MenuItem;
                        }
                        MenuItem nextEnabledItem = base.GetNextEnabledItem(prevEnabledItem.MenuItems, 0);
                        if (nextEnabledItem != null)
                        {
                            return nextEnabledItem;
                        }
                        CancelSourceEventArgs e = new CancelSourceEventArgs
                        {
                            Source = prevEnabledItem,
                            Cancel = false
                        };
                        prevEnabledItem.OnSubmenuOpening(e);
                        return prevEnabledItem;
                    }
            }
            return (base.GetPrevEnabledItem(base.MenuItem.MenuItems, base.MenuItem.MenuItems.Count - 1) ?? base.MenuItem);
        }

        public override void OnMouseClick(object sender, MouseEventArgs e)
        {
            if (base.MenuItem.IsSubmenuOpen)
            {
                CancelSourceEventArgs args = new CancelSourceEventArgs
                {
                    Source = base.MenuItem,
                    Cancel = false
                };
                base.MenuItem.OnSubmenuClosing(args);
            }
            else
            {
                CancelSourceEventArgs args2 = new CancelSourceEventArgs
                {
                    Source = base.MenuItem,
                    Cancel = false
                };
                base.MenuItem.OnSubmenuOpening(args2);
            }
        }

        public override void OnMouseEnter(object sender, MouseEventArgs e)
        {
            base.MenuItem.IsMouseOver = true;
            if (base.MenuItem.Menu.IsSubmenuOpen)
            {
                base.ResetIsSubmenuOpenVisuals(base.MenuItem);
                CancelSourceEventArgs args = new CancelSourceEventArgs
                {
                    Source = base.MenuItem,
                    Cancel = false
                };
                base.MenuItem.OnSubmenuOpening(args);
            }
        }

        public override void OnMouseLeave(object sender, MouseEventArgs e)
        {
            base.MenuItem.IsMouseOver = false;
        }

        public override void ShowSubItems()
        {
            Popup popup = base.MenuItem._elementSubItemsPopup;
            if (!base.MenuItem.Menu.DetectBoundaries)
            {
                if (base.MenuItem.Menu.Orientation == Orientation.Vertical)
                {
                    TranslateTransform transform = new TranslateTransform
                    {
                        X = base.MenuItem.ActualWidth,
                        Y = -base.MenuItem.ActualHeight
                    };
                    popup.RenderTransform = transform;
                }
                else
                {
                    popup.ClearValue(UIElement.RenderTransformProperty);
                }
            }
            popup.IsOpen = true;
        }
    }
}
